package com.lx.controller;


import com.github.pagehelper.PageInfo;
import com.lx.pojo.Team;
import com.lx.service.TeamService;
import com.lx.vo.QueryVo;

import com.lx.vo.ResultVo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("team")
@ResponseBody
public class TeamController {

    @Resource
    private TeamService teamService;

    //如果实体类中的日期类型需要从页面获取数据，避免NULL转换为Date类型出问题
    //解决方案1：@InitBinder
    //解决方案2：实体类的日期类型属性上添加注解@DateTimeFormat(pattern = "yyyy-MM-dd")
    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo queryByPage(Integer pageNum, Integer pageSize, QueryVo vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageNum <= 0) {
            pageSize = 5;
        }
        PageInfo<Team> teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo(teamPageInfo);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVo addTeam(Team team) {
        int i = teamService.addTeam(team);
        if (i == 1) {
            return new ResultVo();
        }
        return new ResultVo(500, "添加失败，服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVo queryByTeamId(@PathVariable("id") Integer teamId) {
        Team team = teamService.queryByTeamId(teamId);
        return new ResultVo<>(team);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVo<Team> updateTeam(@PathVariable("id") Integer teamId, Team team) {
        System.out.println("updateTeam--team-------------" + team);
        team.setTeamId(teamId);
        int i = teamService.updateTeam(team);
        if (i == 1) {
            return new ResultVo<>();
        }
        return new ResultVo<>(500, "服务器内部异常，请稍后再试！");
    }


    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVo deleteTeam(@PathVariable("id") Integer teamId) {
        int i = teamService.deleteTeam(teamId);
        if (i == 1) {
            return new ResultVo();
        }
        return new ResultVo(500, "后台删除出错，请稍后再试");
    }

    @RequestMapping(value = "{id}")
    public ResultVo upLoadLogo(@RequestParam("logo") MultipartFile file, HttpServletRequest httpServletRequest,
                               @PathVariable("id") Integer teamId) {
        //@RequestParam用来获取前段页面中与元素的name相同的值
        //@PathVariable用来获取路径中的值
        //指定文件保存路径
        String realPath = httpServletRequest.getServletContext().getRealPath("/img/uploadFile");
        //获取原始文件名，获取后缀
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String fileExtension = originalFilename.substring(index);
        //生成新的随机文件名避免重复
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newName = uuid + fileExtension;
        //保存文件
        try {
            file.transferTo(new File(realPath+"/"+newName));
            //更新数据库中的文件名
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(newName);
            int i = teamService.updateTeam(team);
            //返回更新结果
            if (i == 1) {
                return new ResultVo();
            } else {
                return new ResultVo(500,"后台更新出错，请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo(500,"上传文件出错！"+e.getMessage());

        }

    }
}
